const app = getApp();
Page({
  data:{
    order:null,
    first:true, //是否是第一次支付
    ticketType: '',
    checked: true,
    mobileNumber: '', //  手机号
    money: 3000,
  },
  onLoad(params){
    const paramsObj = JSON.parse(decodeURIComponent(params.paramsStr))
    const diffTime = Math.floor((new Date(paramsObj.payTimeEnd) - new Date())); //计时器
    paramsObj.diffTime = diffTime;  // 倒计时时间
    this.initData({
      ...paramsObj, 
      priceDiff: (paramsObj.totalPrice-paramsObj.ticketNum*paramsObj.ticketOriginalPrice ).toFixed(2),  // 优惠的差额
    })
    this.setData({mobileNumber: app.globalData.userInfo.mobileNumber})
  },
  initData(params){
    this.setData({
      order:params
    })
  },
  radioChange(e) {
    const radio = e.detail.value;
    const { ticketNum, ticketOriginalPrice, ticketUnitPrice }  = this.data.order;
    console.log(radio, typeof radio);
    console.log(ticketNum * (radio ==='0' ? ticketUnitPrice : ticketOriginalPrice ));
      this.setData({
        order: {
          ...this.data.order,
          price: ticketNum * (radio ==='0' ? ticketUnitPrice : ticketOriginalPrice )
        }
      })
  },
  //支付
  onSubmit(){
    console.log(this.data.order);
    if(!this.data.mobileNumber) {
      wx.showToast({
        icon: 'none',
        title: '请输入手机号码',
      })
      return
    }
    let parms = { 
      ...this.data.order, 
      acceptAdjust: this.data.checked ? 0 : 1,
      mobileNumber: this.data.mobileNumber
    }
    //避免重复支付
    if(this.data.first){
      var that = this;
      wx.getStorage({
        key: 'userInfo',
        success: function (res1) {
          wx.request({
            url: app.globalData.url +`/movieOrder/submitMovieOrder`,
            method: 'POST',
            header:{
              consumerId: app.globalData.userInfo.consumerId
            },
            data:parms,
            success(res) {
              console.log(res.data.data);
              if(res.data.code === 200) {
                const payParam = JSON.parse(res.data.data.payParam);
                console.log(payParam);
                wx.requestPayment({
                  timeStamp: payParam.timeStamp,
                  nonceStr: payParam.nonceStr,
                  package: res.data.data.paySource == 'WX' ? payParam.packageVal : payParam.package,
                  signType: payParam.signType,
                  paySign: payParam.paySign,
                  success (res) { 
                    wx.showToast({
                      title: '支付成功',
                    })
                    wx.switchTab({
                      url: '/pages/top/top',
                    })
                  },
                  fail (res) { }
                })
              }
               else {
                wx.showToast({
                  icon: 'none',
                  title: res.data.msg,
                })
              }

            },
            fail(err) {
              reject(err)
            }
          })
          
        },
        fail: function (res2) {
          wx.showModal({
            content: '您还未登录',
            success(res) {
              wx.switchTab({
                url: '../../user/user',
              })
            }
          })
        }
          
      })
    } else {
      wx.showToast({
        title: '已支付',
        icon:'none'
      })
    }
  },
  onSwitchChange({ detail }) {
    // 需要手动对 checked 状态进行更新
    this.setData({ checked: detail });
  },
})